package com.tanhua.dubbo.api;

import com.tanhua.model.mongo.Follow;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

@DubboService
public class FollowApiImpl implements FollowApi{
    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public Boolean following(Long userId,Long lookId) {
        Criteria criteria = Criteria.where("userId").is(userId)
                .and("lookId").is(lookId);
        Query query = Query.query(criteria);
        return mongoTemplate.exists(query, Follow.class);

    }

    @Override
    public void save(Follow follow) {
        mongoTemplate.save(follow);
    }

    @Override
    public void delete(Follow follow) {
        Long userId = follow.getUserId();
        Long lookId = follow.getLookId();
        Criteria criteria = Criteria.where("userId").is(userId)
                .and("lookId").is(lookId);
        Query query = Query.query(criteria);
        mongoTemplate.remove(query,Follow.class);
    }
}
